<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>可被3整除的最大和</title>
</head>
<body>
  <script>
    var nums = [3, 6, 5, 1, 8]
    function fn(nums) {
      let state = [0, Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER]

      for (let num of nums) {
        if (num % 3 === 0) {
          state = [state[0] + num, state[1] + num, state[2] + num]
        } else if (num % 3 === 1) {
          const a = Math.max(state[2] + num, state[0])
          const b = Math.max(state[0] + num, state[1])
          const c = Math.max(state[1] + num, state[2])
          state = [a, b, c]
        } else if (num % 3 === 2) {
          const a = Math.max(state[1] + num, state[0])
          const b = Math.max(state[2] + num, state[1])
          const c = Math.max(state[0] + num, state[2])
          state = [a, b, c]
        }
      }
      return state[0]
    }
    console.log(fn(nums))
  </script>
</body>
</html>